package com.pakazaka.controllers;

import com.pakazaka.enums.MoveDirectionType;
import com.pakazaka.enums.PlayerStateType;
import com.pakazaka.layer.TiledLayer;
import com.pakazaka.simplecrawler.Constants;
import com.pakazaka.player.Player;
import com.pakazaka.simplecrawler.Core;
import static com.pakazaka.simplecrawler.Core.getInstance;
import java.awt.event.KeyEvent;

/**
 * Process player movement with arrow keys.
 *
 * @author Riseremi
 */
public class MovementController {

    public static void processMovement(KeyEvent e) {
        Player player = getInstance().getPlayer();
        TiledLayer layer = getInstance().getLayer();
        Core core = getInstance();

        /*
         * If map placed at (0, 0) then player just moves up.
         * Else if map scrolled up (for example, placed at (0, -8)),
         * player stay in the center of the screen and map scrolls down.
         */
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            if ((layer.getBlocksY() < 0) && (player.getBlocksY() == Constants.gameHeight >> 1)) {
                if (player.canMove(MoveDirectionType.UP, layer)) {
                    layer.setBlocksY(layer.getBlocksY() + 1);
                }
            } else {
                if (player.canMove(MoveDirectionType.UP, layer)) {
                    player.moveUp(layer);
                }
            }
        }

        /*
         * If map placed at (0, 0) then player just moves down
         * until he reached a center of the screen. Then map scrolls up.
         */
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            if (layer.getBlocksY() > -(layer.getVerticalTilesNumber() - Constants.gameHeight) && (player.getBlocksY() == Constants.gameHeight >> 1)) {
                if (player.canMove(MoveDirectionType.DOWN, layer)) {
                    layer.setBlocksY(layer.getBlocksY() - 1);
                }
            } else {
                if (player.canMove(MoveDirectionType.DOWN, layer)) {
                    player.moveDown(layer);
                }
            }
        }

        //left
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            if ((layer.getBlocksX() < 0) && (player.getBlocksX() == Constants.gameWidth >> 1)) {
                if (player.canMove(MoveDirectionType.LEFT, layer)) {
                    layer.setBlocksX(layer.getBlocksX() + 1);
                }
            } else {
                if (player.canMove(MoveDirectionType.LEFT, layer)) {
                    player.moveLeft(layer);
                }
            }
        }

        //right
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            if (layer.getBlocksX() > -(layer.getHorizontalTilesNumber() - Constants.gameWidth) && (player.getBlocksX() == Constants.gameWidth >> 1)) {
                if (player.canMove(MoveDirectionType.RIGHT, layer)) {
                    layer.setBlocksX(layer.getBlocksX() - 1);
                }
            } else {
                if (player.canMove(MoveDirectionType.RIGHT, layer)) {
                    player.moveRight(layer);
                }
            }
        }

        core.repaint();
    }
}
